#include "EventManager.h"


EventManager::EventManager(void)
{
}


EventManager::~EventManager(void) { clearEvents(); }

void EventManager::update(const GameTime& p_gameTime)
{
	if (!m_destroyList.empty())
	{
		for (auto it = m_destroyList.begin(); it != m_destroyList.end(); it++)
		{
			removeEvent((*it));
		}
		m_destroyList.clear();
	}

	if (!m_events.empty())
	{
		for (auto it = m_events.begin(); it != m_events.end(); it++)
		{
			(*it)->update(p_gameTime);
			if ((*it)->destroy())
				m_destroyList.push_back((*it));
		}
	}
}

inline void EventManager::removeEvent(vn::Event* p_event)
{
	for (auto it = m_events.begin(); it != m_events.end(); it++)
	{
		if ((*it) == p_event)
		{
			m_events.erase(it);
			delete p_event;
			break;
		}
	}
}

void EventManager::clearEvents()
{
	if (!m_events.empty())
	{
		for (auto it = m_events.begin(); it != m_events.end(); it++)
		{
			delete (*it);
			(*it) = nullptr;
		}
		m_events.clear();
	}
}